<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\TeamModel;
use App\Models\MatchModel;
use App\Models\PlayerModel;
use App\Models\DataModel;
class DataController extends Controller
{
    public function dataAdd($match_id){
    	 //获取比赛的球队
        $team1=MatchModel::join('team','match.team_one','team.team_id')->orderBy('match_id','asc')->where('match_id',$match_id)->get()->toArray();
        $team2=MatchModel::join('team','match.team_two','team.team_id')->orderBy('match_id','asc')->where('match_id',$match_id)->get()->toArray();
        //var_dump($team2);
        foreach($team1 as $k=>$v){
            $team1[$k]['team_name_b']=$team2[$k]['team_name'];
        }
        //var_dump($team1);die;
        //获取比赛成员
        $team=MatchModel::where('match_id',$match_id)->first();
        $team_one=$team['team_one'];
        $team_two=$team['team_two'];
        $team_one_info=PlayerModel::join('team','player.team_id','=','team.team_id')->where('player.team_id',$team_one)->get()->toArray();
        $team_two_info=PlayerModel::join('team','player.team_id','=','team.team_id')->where('player.team_id',$team_two)->get()->toArray();
        //var_dump($team_two_info);
        $data=array_merge($team_one_info,$team_two_info);
       // var_dump($data);die;
        return view("admin/dataAdd",['team1'=>$team1,'data'=>$data]);
    }
    //数据执行添加
    public function dataCreate(){
        $data=request()->except("_token");
        $res=DataModel::create($data);
        if($res){
            return redirect("admin/dataIndex");
        }
    }


    public function dataIndex(){

    	$data=DataModel::join("match","match.match_id","=","data.match_id")->get()->toArray();
    	
    	$data1=MatchModel::select('match.match_id','match.start_time','tA.team_name as tA_name','tB.team_name as tB_name')
    ->join('team as tA','match.team_one',"=",'tA.team_id')
    ->join('team as tB','match.team_two',"=",'tB.team_id')
    ->get();
    	dd($data);
    }
}
